<?php
/**
 * Created by PhpStorm.
 * User: 陈艺赫
 * Date: 2017/12/6
 * Time: 0:19
 */

namespace Admin\Controller;


use Think\Controller;

class LoginController extends Controller
{
    public function index()
    {
        if (I('post.')) {
            $user_name = I('post.username');
            $password = I('post.password');
            $db_user = M('user');
            $map['mail'] = $user_name;
            $map['password'] = sha1(md5($password));
            $userinfo = $db_user->where($map)->find();


            if ($user_name == '' || $password == '') {
                $this->error('账号或密码不能为空!', U('Login/index'), TRUE);
            }
            //是否选择记住我的信息
            if (!empty(I('post.auto')) && (I('post.auto') == 'auto')) {
                $value = $user_name . '|' . get_client_ip() . '|' . $password . '|' . I('post.auto');
                $value = encrypt($value, 'E', 'a');
                $time = time() + 3600 * 24 * 7;// 有效期一星期
                //@表示屏蔽报错
                @setcookie('auto', $value, $time, '/');
            } else {
                setcookie('auto', '', time() - 3600, '/');
            }

            if (!empty($userinfo)) {
                session('user', $userinfo);
                session('last_login_time', time());
//                $data['id'] = $userinfo['id'];
//                $data['last_activity'] = '';
//                $data['info'] = get_client_ip();
//                $result = $db_user->where('id = ' . $userinfo['id'])->save($data);
//                dump($result);
                $this->success('登录成功,正在跳转...', U('Index/index'), TRUE);
            } else {
                $this->error('用户名或密码错误！', '', TRUE);
            }

        } else {
            $this->display();
        }

    }
    //退出登录
    public function logout(){
        session(null);
        $this -> redirect('Login/index');
    }

}